package com.guigu.erp.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.erp.dto.DesignModuleDto;
import com.guigu.erp.dto.ManufactureDesignProcedureDto;
import com.guigu.erp.dto.Vo;
import com.guigu.erp.mapper.ManufactureDesignProcedureDetailsMapper;
import com.guigu.erp.mapper.ManufactureDesignProcedureMapper;
import com.guigu.erp.pojo.*;
import com.guigu.erp.service.DesignFileService;
import com.guigu.erp.service.ManufactureDesignProcedureDetailsService;
import com.guigu.erp.service.ManufactureDesignProcedureModuleService;
import com.guigu.erp.service.ManufactureDesignProcedureService;
import com.guigu.erp.utils.PageUtils;
import com.guigu.erp.vo.ModuleFileVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * ClassName:ManufactureDesignProcedureServiceImpl
 * Package:com.guigu.erp.service.impl
 * Description:
 *
 * @Date:2024/3/12 0:26
 * @Author:chenchong
 */
@Service
public class ManufactureDesignProcedureServiceImpl extends ServiceImpl<ManufactureDesignProcedureMapper, ManufactureDesignProcedure> implements
        ManufactureDesignProcedureService  {

@Autowired
private ManufactureDesignProcedureMapper manufactureDesignProcedureMapper;
@Autowired
private ManufactureDesignProcedureService manufactureDesignProcedureService;
@Autowired
private ManufactureDesignProcedureDetailsService manufactureDesignProcedureDetailsService;

    @Autowired
    private DesignFileService designFileService;
    @Override
    public String getMaxprocedure() {
        ManufactureDesignProcedure designModule = manufactureDesignProcedureMapper.queryMaxProcedure();
        if(designModule==null){
            return null;
        }
        String designId = designModule.getDesignId();
        return designId;
    }

    @Transactional
    @Override
    public void makeDesignProcedure(ManufactureDesignProcedureDto manufactureDesignProcedureDto) {
            //添加产品物料表
        manufactureDesignProcedureService.save(manufactureDesignProcedureDto);
            //添加产品物料明细表
        List<ManufactureDesignProcedureDetails> details = manufactureDesignProcedureDto.getDetails();
        for(int i=0;i<details.size();i++){

            ManufactureDesignProcedureDetails manufactureDesignProcedureDetails = details.get(i);
            if(manufactureDesignProcedureDetails.getAmountUnit()==null){
                manufactureDesignProcedureDetails.setAmountUnit("小时");
            }
            //每条id明细设置为null
            manufactureDesignProcedureDetails.setId(null);
                //获取工序设计单ID
                int id = manufactureDesignProcedureDto.getId();
                //设置父级ID
            manufactureDesignProcedureDetails.setParentId(id);
                //设置工序序号
            manufactureDesignProcedureDetails.setDetailsNumber(i+1);


//            当前工序物料标志
//            D002-0: 未设计
//            D002-1: 已设计
            manufactureDesignProcedureDetails.setDesignModuleTag("D002-0");
//            当前工序物料变更标志
//            D003-0: 未变更
//            D003-0: 已变更amountUnit
            manufactureDesignProcedureDetails.setDesignModuleChangeTag("D003-0");

                // 插入工序明细
            manufactureDesignProcedureDetailsService.save(manufactureDesignProcedureDetails);

            }
//        工序组成标志
//        G001-0: 未设计
//        G001-1: 已设计
            String productId =manufactureDesignProcedureDto.getProductId();
            QueryWrapper<DesignFile> qw=new QueryWrapper<DesignFile>();
            qw.eq("product_id",productId);
            DesignFile designFile =designFileService.getOne(qw);
            designFile.setDesignProcedureTag("G001-1");
            designFileService.updateById(designFile);
        }


    @Override
    public PageUtils<ManufactureDesignProcedure> page()
    {
        QueryWrapper<ManufactureDesignProcedure> qw=new QueryWrapper<>();
        qw.eq("CHECK_TAG", "S001-0");
        qw.eq("CHANGE_TAG", "B002-0");
        qw.eq("DESIGN_MODULE_TAG", "G002-0");
        qw.eq("DESIGN_MODULE_CHANGE_TAG", "G003-0");

        List<ManufactureDesignProcedure> list = manufactureDesignProcedureMapper.selectList(qw);
        PageInfo<ManufactureDesignProcedure> p=new PageInfo<>(list);
        PageUtils<ManufactureDesignProcedure> utils=new PageUtils<>();
        utils.setTotal(p.getTotal());
        utils.setRows(p.getList());
        return utils;
    }

    @Override
    public PageUtils<ManufactureDesignProcedureDetails> showDetails(int id) {
        QueryWrapper<ManufactureDesignProcedureDetails> qw=new QueryWrapper<>();
        qw.eq("PARENT_ID", id);
        List<ManufactureDesignProcedureDetails> list = manufactureDesignProcedureDetailsService.list(qw);
        PageInfo<ManufactureDesignProcedureDetails> p=new  PageInfo<>(list);
        PageUtils<ManufactureDesignProcedureDetails> utils=new PageUtils<>();
        utils.setTotal(p.getTotal());
        utils.setRows(p.getList());
        return utils;

    }

    @Override
    public PageUtils<ManufactureDesignProcedure> procedureShow(ModuleFileVo vo, int pageNo, int pageSize) {
        QueryWrapper<ManufactureDesignProcedure> qw = new QueryWrapper<>();
        if (vo != null) {
            if (!StringUtils.isEmpty(vo.getSearch_firstKindId())) {
                qw.eq("FIRST_KIND_ID", vo.getSearch_firstKindId());
            }
            if (!StringUtils.isEmpty(vo.getSearch_secondKindId())) {
                qw.eq("SECOND_KIND_ID", vo.getSearch_secondKindId());
            }
            if (!StringUtils.isEmpty(vo.getSearch_thirdKindId())) {
                qw.eq("THIRD_KIND_ID", vo.getSearch_thirdKindId());
            }
            if (!StringUtils.isEmpty(vo.getSearch_productName())) {
                qw.eq("PRODUCT_NAME", vo.getSearch_productName());
            }
            if (!StringUtils.isEmpty(vo.getSearch_checkTag())) {
                qw.eq("CHECK_TAG", vo.getSearch_checkTag());
            }
            if (!StringUtils.isEmpty(vo.getSearch_registerTime1())) {
                qw.ge("REGISTER_TIME", vo.getSearch_registerTime1());
            }
            if (!StringUtils.isEmpty(vo.getSearch_registerTime2())) {
                qw.le("REGISTER_TIME", vo.getSearch_registerTime2());
            }
        }
        PageHelper.startPage(pageNo, pageSize);
        List<ManufactureDesignProcedure> list = manufactureDesignProcedureMapper.selectList(qw);
        PageInfo<ManufactureDesignProcedure> pageInfo = new PageInfo<>(list);
        // PageInfo 转换成 PageUtils
        PageUtils<ManufactureDesignProcedure> pageUtils = new PageUtils<>();
        pageUtils.setRows(pageInfo.getList());
        pageUtils.setTotal(pageInfo.getTotal());

        return pageUtils;
    }
    @Override
    public PageUtils<ManufactureDesignProcedure> procedureShow10() {
        QueryWrapper<ManufactureDesignProcedure> qw=new QueryWrapper<>();
        qw.eq("CHECK_TAG", "S001-1");
        qw.eq("CHANGE_TAG", "B002-0");
        qw.eq("DESIGN_MODULE_TAG", "G002-0");
        qw.eq("DESIGN_MODULE_CHANGE_TAG", "G003-0");

        List<ManufactureDesignProcedure> list = manufactureDesignProcedureMapper.selectList(qw);
        PageInfo<ManufactureDesignProcedure> p=new PageInfo<>(list);
        PageUtils<ManufactureDesignProcedure> utils=new PageUtils<>();
        utils.setTotal(p.getTotal());
        utils.setRows(p.getList());
        return utils;
    }
    @Override
    public PageUtils<ManufactureDesignProcedure> procedureShow11(ModuleFileVo vo,int pageNo, int pageSize) {

        QueryWrapper<ManufactureDesignProcedure> qw=new QueryWrapper<>();
//        qw.eq("DESIGN_MODULE_TAG", "G002-1").or().eq("DESIGN_MODULE_TAG", "G002-2");
        if (vo != null) {
            if (!StringUtils.isEmpty(vo.getSearch_firstKindId())) {
                qw.eq("FIRST_KIND_ID", vo.getSearch_firstKindId());
            }
            if (!StringUtils.isEmpty(vo.getSearch_secondKindId())) {
                qw.eq("SECOND_KIND_ID", vo.getSearch_secondKindId());
            }
            if (!StringUtils.isEmpty(vo.getSearch_thirdKindId())) {
                qw.eq("THIRD_KIND_ID", vo.getSearch_thirdKindId());
            }
            if (!StringUtils.isEmpty(vo.getSearch_productName())) {
                qw.eq("PRODUCT_NAME", vo.getSearch_productName());
            }
            if (!StringUtils.isEmpty(vo.getSearch_checkTag())) {
                qw.eq("DESIGN_MODULE_TAG", vo.getSearch_checkTag());
            }
            if (!StringUtils.isEmpty(vo.getSearch_registerTime1())) {
                qw.ge("REGISTER_TIME", vo.getSearch_registerTime1());
            }
            if (!StringUtils.isEmpty(vo.getSearch_registerTime2())) {
                qw.le("REGISTER_TIME", vo.getSearch_registerTime2());
            }
        }

        PageHelper.startPage(pageNo, pageSize);
        List<ManufactureDesignProcedure> list = manufactureDesignProcedureMapper.selectList(qw);
        PageInfo<ManufactureDesignProcedure> p=new PageInfo<>(list);
        PageUtils<ManufactureDesignProcedure> utils=new PageUtils<>();
        utils.setTotal(p.getTotal());
        utils.setRows(p.getList());
        return utils;
    }

    @Override
    public PageUtils<ManufactureDesignProcedure> designModuleShow() {
        QueryWrapper<ManufactureDesignProcedure> qw = new QueryWrapper<>();
        qw.eq("DESIGN_MODULE_TAG", "G002-1");
//        qw.eq("CHECK_TAG", "S001-0");
        List<ManufactureDesignProcedure> list = manufactureDesignProcedureService.list(qw);
        PageInfo<ManufactureDesignProcedure>  p=new  PageInfo<>(list);
        PageUtils<ManufactureDesignProcedure> utils=new PageUtils<>();
        utils.setRows(p.getList());
        utils.setTotal(p.getTotal());
        return utils;
    }


}


